package me.numark.activities;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import butterknife.Bind;
import me.numark.R;
import me.numark.fragments.FriendsFragment;

public class FollowsActivity extends BaseActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    @Bind(R.id.back)
    ImageView back;

    @Bind(R.id.pager)
    ViewPager pager;

    @Bind(R.id.radio_left)
    RadioButton leftRadio;

    @Bind(R.id.radio_right)
    RadioButton rightRadio;

    @Bind(R.id.radio_group)
    RadioGroup radioGroup;

    FollowsFragmentPagerAdapter mAdapter;
    public static final String USER_ID = "user_id";
    int uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.fragment_tabs;
        super.onCreate(savedInstanceState);
        uid = getIntent().getIntExtra(USER_ID, -1);
        uiInit();
    }

    protected void uiInit() {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        mAdapter = new FollowsFragmentPagerAdapter();
        pager.setAdapter(mAdapter);
        pager.addOnPageChangeListener(this);
        leftRadio.setText(mAdapter.getPageTitle(0));
        rightRadio.setText(mAdapter.getPageTitle(1));
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 1)
            radioGroup.check(R.id.radio_right);
        else if (position == 0)
            radioGroup.check(R.id.radio_left);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radio_right) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    private class FollowsFragmentPagerAdapter extends FragmentPagerAdapter {

        public FollowsFragmentPagerAdapter() {
            super(getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 1)
                return FriendsFragment.newInstance(uid, false, FriendsFragment.TYPE_BOTH);
            else
                return FriendsFragment.newInstance(uid, false, FriendsFragment.TYPE_FOLLOW);

        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.follows);
                case 1:
                    return getString(R.string.mutual_concern);
                default:
                    return "";
            }
        }
    }
}
